package com.ossean.match.main;
import org.apache.lucene.queryparser.classic.ParseException;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import org.springframework.stereotype.Component;

import com.ossean.match.lucene.LuceneIndex;
import com.ossean.match.matchprocess.MatchIncrement;
import com.ossean.match.matchprocess.MemoToPrjMatch;
import com.ossean.match.matchprocess.NewPrjMonitor;
import com.ossean.match.matchprocess.PrjToMemoMatch;

@Component
public class Main {
	@Qualifier("newprjmonitor")
	@Autowired
	private NewPrjMonitor newPrjMonitor;
	
	@Qualifier("memotoprjmatch")
	@Autowired
	private MemoToPrjMatch memoToPrjMatch;
	
	@Qualifier("prjtomemomatch")
	@Autowired
	private PrjToMemoMatch prjToMemoMatch;
	
	@Qualifier("luceneindex")
	@Autowired
	private LuceneIndex luceneIndex;
	
	private Logger logger = LoggerFactory.getLogger(getClass());
	
	public static void main(String[] args) throws InterruptedException, ParseException {
		@SuppressWarnings("resource")
		ApplicationContext applicationContext = new ClassPathXmlApplicationContext("classpath:/applicationContext_mybatis.xml");
		Main mainClass = applicationContext.getBean(Main.class);
		mainClass.start();
	}
	
	public void start() throws InterruptedException, ParseException {
		while (true) {
			MatchIncrement matchIncrement = new MatchIncrement();
			newPrjMonitor.setMatchIncrement(matchIncrement);
			memoToPrjMatch.setMatchIncrement(matchIncrement);
			prjToMemoMatch.setMatchIncrement(matchIncrement);
			
			if (matchIncrement.getMemoHistory() == 0) {
				luceneIndex.run(matchIncrement);
			}
			
			newPrjMonitor.run();
			prjToMemoMatch.run();
			memoToPrjMatch.run();
			
		    logger.info(".........sleeping.........." + 1000*60 + "s......");
		    Thread.sleep(1000*60);
		}
	}
}
